package com.eco.fanliapp.taobao;


import com.alibaba.fastjson.JSON;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkPrivilegeGetRequest;
import com.taobao.api.request.TbkScActivitylinkToolgetRequest;
import com.taobao.api.response.TbkPrivilegeGetResponse;
import com.taobao.api.response.TbkScActivitylinkToolgetResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Service
public class SessionController {



    /**
     * 根据商品id高佣转链
     * @param itmeId
     * @param siteId
     * @param adzoneid
     * @param session
     * @return
     */
    @RequestMapping(value = "/privilege/{itmeId}/{siteId}/{adzoneid}/{session}", method = RequestMethod.GET)
    public TbkPrivilegeGetResponse tbkPrivilegeGetResponse(@PathVariable("itmeId") Long itmeId,
                                                           @PathVariable("siteId") Long siteId,
                                                           @PathVariable("adzoneid") Long adzoneid,
                                                           @PathVariable("session") String session) {
        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkPrivilegeGetRequest req = new TbkPrivilegeGetRequest();
        req.setItemId(itmeId);
        req.setAdzoneId(adzoneid);
        req.setSiteId(siteId);
        TbkPrivilegeGetResponse rsp = null;
        try {
            rsp = client.execute(req, session);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return rsp;
    }
    public static void main(String[] args) {

        // 设置过期时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, 12);
        Date time = calendar.getTime();
//        Date dd = new Date(System.currentTimeMillis() + 30 * 24 * 60 * 60 * 1 * 1000);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//          String sd = sdf.format(dd); // 时间戳转换日期



        Date dd1 = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24  * 365L);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String sd1 = sdf.format(dd1); // 时间戳转换日期

//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        try {
//            String aa = DateFormatUtils.dateToStamp("2019-08-01");
//            String cc = String.valueOf(simpleDateFormat.parse("2019-08-01").getTime());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }



        SessionController sessionController = new SessionController();
//        TbkPrivilegeGetResponse re = sessionController.tbkPrivilegeGetResponse(599995743280L, Long.parseLong("mm_339150057_530100389_109291550327".split("_")[2]),
//                Long.parseLong("mm_339150057_530100389_109291550327".split("_")[3]), "61016204d0d8077c58b730efcb7217bcf24b1ba415d9c113322183257");

        TbkScActivitylinkToolgetResponse re = sessionController.tbkScActivitylinkToolgetResponse(Long.parseLong("mm_112871851_42594477_239820486".split("_")[2]),
                Long.parseLong("mm_112871851_42594477_239820486".split("_")[3]), "6102721a1d6a16aa6109a527f4efc118d3d7d8524af391f1064809111", "2348088484", 1577771853634L);
        System.out.println(JSON.toJSONString(re));
    }
    /**
     * 高佣转链
     * @param me
     * @param siteId
     * @param adzoneid
     * @param session
     * @return
     */
    @RequestMapping(value = "/privilegeme/{me}/{siteId}/{adzoneid}/{session}", method = RequestMethod.GET)
    public TbkPrivilegeGetResponse tbkPrivilegeGetResponseMe(@PathVariable("me") String me,
                                                             @PathVariable("siteId") Long siteId,
                                                             @PathVariable("adzoneid") Long adzoneid,
                                                             @PathVariable("session") String session) {
        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkPrivilegeGetRequest req = new TbkPrivilegeGetRequest();
        req.setMe(me);
        req.setAdzoneId(adzoneid);
        req.setSiteId(siteId);
        TbkPrivilegeGetResponse rsp = null;
        try {
            rsp = client.execute(req, session);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return rsp;
    }


    /**
     * 根据活动id和用户id进行高佣转链
     * @param siteId
     * @param adzoneid
     * @param session
     * @param relationId
     * @param promotionSceneId
     * @return
     */
    public TbkScActivitylinkToolgetResponse tbkScActivitylinkToolgetResponse(     @PathVariable("siteId") Long siteId,
                                                           @PathVariable("adzoneid") Long adzoneid,
                                                           @PathVariable("session") String session,
                                                           @PathVariable("relationId") String relationId,
                                                           @PathVariable("promotionSceneId") Long promotionSceneId
    ) {
        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkScActivitylinkToolgetRequest req = new TbkScActivitylinkToolgetRequest();
        req.setAdzoneId(adzoneid);
        req.setSiteId(siteId);
        req.setPlatform(2L);
        if (relationId != null && !"".equals(relationId) && !"0".equals(relationId)) {
            req.setRelationId(relationId);
        }
        req.setPromotionSceneId(promotionSceneId);
        TbkScActivitylinkToolgetResponse rsp = null;
        try {
            rsp = client.execute(req, session);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return rsp;
    }

}
